﻿/*
 * Programming Graphical User Interfaces
 * Example program: Diagram
 * DiagramView.cs: Implements a graphical view of the diagram.
 * Jarkko Leponiemi 2004
 */

using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using SharppiFlowEditor.models;
using SharppiFlowEditor.views;

namespace SharppiFlowEditor.controllers
{
    /*
     * The digram controller for moving existing nodes.
     */
    public class MoveNodeController : AbstractController
    {

        private float offsx, offsy;
        private AbstractNode moving = null;


        public MoveNodeController(AppView appView, AppModel appModel)
        {
            Model = appModel;
            View = appView;
        }

        /*
         * Start moving a node.
         */
        override public void ViewMouseDown(object sender, MouseEventArgs e)
        {
            // get the possible moving node

            scrollOffset = View.document.ViewScrollPosition;
            zoomValue = View.document.ZoomSize;

            moving = View.document.GetNode((e.X - scrollOffset.X), (e.Y - scrollOffset.Y));
            if (moving != null)
            {
                // calculate offsets from the current point 
                // to the center point
                offsx = (int)((e.X - scrollOffset.X) / zoomValue) - moving.getBoundingRect().X;
                offsy = (int)((e.Y - scrollOffset.Y) / zoomValue) - moving.getBoundingRect().Y;
            }
            
        }


        
        /*
         * Continue moving the node.
         */
        override public void ViewMouseMove(object sender, MouseEventArgs e)
        {
            scrollOffset = View.document.ViewScrollPosition;
            zoomValue = View.document.ZoomSize;

            if (moving != null)
            {
                if (View.document.GraphDocument.SnapToGridState.Equals(true))
                {
                    int x = (e.X - scrollOffset.X) - (int)offsx;
                    int y = (e.Y - scrollOffset.Y) - (int)offsy;

                    Model.MoveNode(View.document.GraphDocument, moving, View.document.GraphDocument.SnapX(x), View.document.GraphDocument.SnapY(y));
                }
                else
                {
                    Model.MoveNode(View.document.GraphDocument, moving, (int)(((e.X - scrollOffset.X)) / zoomValue) - offsx, (int)(((e.Y - scrollOffset.Y)) / zoomValue) - offsy);
                }
            }
        }

        /*
         * Stop moving.
         */
        override public void ViewMouseUp(object sender, MouseEventArgs e)
        {

            if (moving != null)
            {
                /*if (View.document.GraphDocument.SnapToGridState.Equals(true))
                {
                    int snapX = e.X - e.X % View.document.GraphDocument.GridWidth;
                    int snapY = e.Y - e.Y % View.document.GraphDocument.GridWidth;
                    Model.MoveNode(View.document.GraphDocument, moving, (snapX - scrollOffset.X) - offsx, (snapY - scrollOffset.Y) - offsy);
                }
                else
                {
                    Model.MoveNode(View.document.GraphDocument, moving, (int)(((e.X - scrollOffset.X)) / zoomValue) - offsx, (int)(((e.Y - scrollOffset.Y)) / zoomValue) - offsy);
                }*/
                moving = null;
            }
        }
    }
}
